package io.renren.modules.user.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.ConstantUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.collect.service.CollectinfosService;
import io.renren.modules.dictionary.entity.DictionaryEntity;
import io.renren.modules.dictionary.service.DictionaryService;
import io.renren.modules.footprint.service.VipfootprintinfosService;
import io.renren.modules.projectinfo.service.ProjectinfosService;
import io.renren.modules.user.entity.UserinfosEntity;
import io.renren.modules.user.service.UserinfosService;
import io.renren.modules.user.util.UserUtil;
import io.renren.modules.vipuser.service.VipinfosService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;




/**
 * 用户表
 * 
 * @author
 * @email 
 * @date 2018-10-18 15:06:56
 */
@RestController
@RequestMapping("/user/userinfos")
public class UserinfosController {
	@Autowired
	private UserinfosService userinfosService;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private VipinfosService vipinfosService;
	@Autowired
	private CollectinfosService collectinfosService;
	@Autowired
	private VipfootprintinfosService vipfootprintinfosService;
	@Autowired
	private ProjectinfosService projectinfosService;

	@SysLog("查询用户简单信息")
	@RequestMapping("desclist")
	@RequiresPermissions("user:userinfos:list")
	public R descList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);

		List<UserinfosEntity> userinfosList = userinfosService.queryDescList(query);
		int total = userinfosService.queryDescTotal(query);

		PageUtils pageUtil = new PageUtils(userinfosList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}


	/**
	 * 列表
	 */
	@SysLog("查询用户列表")
	@RequestMapping("/list")
	@RequiresPermissions("user:userinfos:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<UserinfosEntity> userinfosList = userinfosService.queryList(query);
		int total = userinfosService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(userinfosList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	/**
	 * 信息
	 */
	@SysLog("查询用户信息")
	@RequestMapping("/info/{userinfoid}")
	@RequiresPermissions("user:userinfos:info")
	public R info(@PathVariable("userinfoid") String userinfoid){
		UserinfosEntity userinfos = userinfosService.queryObject(userinfoid);
		return R.ok().put("userinfos", userinfos);
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存用户")
	@RequestMapping("/save")
	@RequiresPermissions("user:userinfos:save")
	public R save(@RequestBody UserinfosEntity userinfos){
		ValidatorUtils.validateEntity(userinfos, AddGroup.class);
		if (userinfos.getUseraccount().length() < 4){
			return R.error("用户账户长度不小于4位");
		}
		//判断输入值是否符合规则
		//积分
		if (userinfos.getIntegralnumber() != null){
			if (userinfos.getIntegralnumber().compareTo(ConstantUtils.MININTEGRALNUMBER) < 0){
				return R.error("积分余额不能为负数");
			}
		}
		//性别
		R usergenderError = userinfosService.judgeIsUsergenderError(userinfos.getUsergender());
		if (usergenderError != null){
			return usergenderError;
		}
		//软删除
		R judgeIsDelError = userinfosService.judgeIsDelError(userinfos.getIsdel());
		if (judgeIsDelError != null){
			return judgeIsDelError;
		}
		//锁定
		R judgeIslockedoutError = userinfosService.judgeIslockedoutError(userinfos.getIslockedout());
		if (judgeIslockedoutError != null){
			return judgeIslockedoutError;
		}

		//判断用户账户是否重复
		int count = userinfosService.isUserAccountRepetition(userinfos.getUseraccount());
		if (count > 0){
			return R.error("用户账户已存在");
		}
		userinfosService.save(userinfos);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改用户")
	@RequestMapping("/update")
	@RequiresPermissions("user:userinfos:update")
	public R update(@RequestBody UserinfosEntity userinfos){
		ValidatorUtils.validateEntity(userinfos, UpdateGroup.class);
		//判断输入值是否符合规则
		if (userinfos.getIntegralnumber().compareTo(ConstantUtils.MININTEGRALNUMBER) < 0){
			return R.error("积分余额不能为负数");
		}
		//性别
		R usergenderError = userinfosService.judgeIsUsergenderError(userinfos.getUsergender());
		if (usergenderError != null){
			return usergenderError;
		}
		//软删除
		R judgeIsDelError = userinfosService.judgeIsDelError(userinfos.getIsdel());
		if (judgeIsDelError != null){
			return judgeIsDelError;
		}
		//锁定
		R judgeIslockedoutError = userinfosService.judgeIslockedoutError(userinfos.getIslockedout());
		if (judgeIslockedoutError != null){
			return judgeIslockedoutError;
		}
		//vip
		R judgeIsVipError = userinfosService.judgeIsVipError(userinfos.getIsvip());
		if (judgeIsVipError != null){
			return judgeIsVipError;
		}
		userinfosService.update(userinfos);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除用户")
	@RequestMapping("/delete")
	@RequiresPermissions("user:userinfos:delete")
	public R delete(@RequestBody String[] userinfoids){
		//删除用户收藏
		collectinfosService.deleteByUser(userinfoids);
		//删除项目
		projectinfosService.deleteByUser(userinfoids);
		//删除足迹
		vipfootprintinfosService.deleteByUser(userinfoids);

		vipinfosService.deleteBatchByUserInfoId(userinfoids);
		userinfosService.deleteBatch(userinfoids);
		
		return R.ok();
	}



	@SysLog("搜索用户")
	@RequestMapping("/queryUser")
	@RequiresPermissions("user:userinfos:list")
	public R queryUser(@RequestParam Map<String, Object> params){
		Query query = new Query(params);

		List<UserinfosEntity> userinfosEntities = userinfosService.queryUserByName(query);

		int total = userinfosService.queryUserByNameTotal(query);

		PageUtils pageUtil = new PageUtils(userinfosEntities, total, query.getLimit(), query.getPage());

		return R.ok().put("page",pageUtil);
	}

	@SysLog("修改用户设置信息")
	@RequestMapping("/updateUserSetInfo")
	@RequiresPermissions("user:userinfos:update")
	public R updateUserSetInfo(@RequestBody DictionaryEntity[] dictionarys){
		//循环修改相应字段
		for(int i= 0;i < dictionarys.length;i++) {
			if (dictionarys[i].getDictionaryid().equals(null)){
				return R.error("Dictionaryid不能为空");
			}
			dictionarys[i].setUpdatetime(new Date());
			dictionaryService.update(dictionarys[i]);
		}
		return R.ok();
	}
	
}
